/*
 * 
 * Created on Jul 29, 2005
 */
package com.wgo.precise.client.ui.view.requirementstable;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import com.wgo.precise.client.ui.controller.ModelStatus;
import com.wgo.precise.client.ui.controller.RequirementPlugin;
import com.wgo.precise.client.ui.model.ClientModelItem;
import com.wgo.precise.client.ui.model.facade.IModelPersistable;
import com.wgo.precise.client.ui.model.facade.INodeParent;
import com.wgo.precise.client.ui.view.editor.requirementtable.TableContentProvider;
import com.wgo.precise.client.ui.view.facade.IExtendedEditorPart;
import com.wgo.precise.client.ui.view.util.EditorInput;
import com.wgo.precise.client.ui.view.util.ModelPersistable;


/**
 * @author Petter L. H. Eide
 */



public class TableEditorPart extends EditorPart implements IExtendedEditorPart {

	private IModelPersistable currentInput = null ;
	private TableViewer viewer = null ;

	public TableEditorPart() {
		super();
	}

	/**
	 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
	 *      org.eclipse.ui.IEditorInput)
	 */
	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		setSite(site);
		setInput(input);
		
//		if (!(input instanceof EditorInput) || !(input.getPersistable() instanceof ModelPersistable)) {
//			throw new PartInitException("Invalid Input: Must be ClientModelItem");
//		}
		
		if ( null != getModelInput() ) {
//			RequirementPlugin.addModelUpdateListener(this);
//			RequirementPlugin.addModelChangedListener(this);
			RequirementPlugin.getInstance().getEditorWindowManager().addExtendedEditor(this);
		} else {
//			RequirementPlugin.removeModelChangedListener(this);
//			RequirementPlugin.removeModelUpdateListener(this);
		}

	}

	/**
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		ModelPersistable modelElement = (ModelPersistable) getEditorInput().getPersistable();
		currentInput = modelElement.getItem();
		viewer = new TableViewer(parent, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
		viewer.setContentProvider(new TableContentProvider());
		viewer.setInput(currentInput);
	}

	/**
	 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
	 */
	@Override
	public void dispose() {
		super.dispose();

//		RequirementPlugin.removeModelUpdateListener(this);
		// RequirementPlugin.getInstance().removeSpecializedEditor(this);

	}


	/**
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
		viewer.getControl().setFocus();
	}

	public void onModelUpdate(IModelPersistable item) {
		// long identifier =
		// ((ModelPersistable)getEditorInput().getPersistable()).getItem().getIdentifier();
		IModelPersistable oldItem = ((ModelPersistable) getEditorInput()
				.getPersistable()).getItem();
		IModelPersistable newItem = oldItem;
		if (null != item.getIdentifier()) {
			newItem = RequirementPlugin.getInstance().getSession()
					.getModelItem(item.getIdentifier());
		}
		// viewer.setInput(newItem);
		setInput(new EditorInput(newItem));
		viewer.setInput(newItem);
		viewer.refresh();
	}

	public void onModelChange(IModelPersistable item) {
	}

	/**
	 * @see com.wgo.precise.client.ui.view.editor.IModelItemFormEditor#getModelInput()
	 */
	public ClientModelItem getModelInput() {
		return null;
	}

	public void inputChanged() {
	}

	public void onModelItemDeleted(IModelPersistable item, INodeParent parent) {
	}

	public void onNewModelItemCreated(IModelPersistable item) {
	}

	/**
	 * @see org.eclipse.ui.part.EditorPart#setInput(org.eclipse.ui.IEditorInput)
	 */
	@Override
	protected void setInput(IEditorInput input) {
		super.setInput(input);
		ModelPersistable modelElement = (ModelPersistable) getEditorInput().getPersistable();
        if (null != modelElement) {
            this.currentInput = modelElement.getItem();
        	updateMetaInformation();
        } else {
			RequirementPlugin.log(new ModelStatus(IStatus.WARNING, getClass().getSimpleName() + ": No valid object in input." ));
        }
	}

	private void updateMetaInformation(){
		if (null != getModelInput()) {
			setPartName(getModelInput().getText());
			setContentDescription(getModelInput().getToolTipText());
			setTitleToolTip(getModelInput().getToolTipText());
			Image image = getModelInput().getImage();
			if (null != image) {
				setTitleImage(image);
			}
		} else {
			RequirementPlugin.log(new ModelStatus(IStatus.ERROR, this.getClass().getSimpleName()+ " Has no valid input" ));
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
	 */
	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#isDirty()
	 */
	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
	 */
	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}


}